import {
  getPersonInfoApi,
  getFamilyMemberApi,
  getHealthManageApi,
  searchFamilyName
} from "../../services/filing/filing"
import {
  GETSTORAGE
} from "../../services/storage/storage"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    familyName: '',
    personageName: '',
    familys: [],
    citys: [],
    loading: false,
    guValue: "gradient",
    // 下拉刷新
    enable: false,
    loadingProps: {
      size: '30rpx',
      inheritColor: true,
      theme: 'spinner'
    },
    backTopVisible: false,
    // 健康饮食
    lines: [
      "全麦面包、馒头、粉、面等富含碳水化物的粮谷类食物",
    ]
  },
  // 下拉刷新
  ready() {
    setTimeout(() => {
      this.setData({
        enable: true
      });
    }, 1000);
  },
  onScroll(e) {
    const {
      scrollTop
    } = e.detail;
    this.setData({
      backTopVisible: scrollTop > 100,
    });
  },
  onRefresh() {
    this.setData({
      enable: true
    });
    this.getInfo()
    setTimeout(() => {
      this.setData({
        enable: false
      });
    }, 1500);
  },
  // 滑动picke
  onColumnChange(e) {
    console.log(e);
    const {
      column,
      value
    } = e.detail
    console.log(e.detail);
    if (column == 1) return
    this.setData({
      citys: []
    })
    getHealthManageApi({
      familyId: value[0]
    }).then(res => {
      console.log(res, '筛选');
      let joinList = res.data.data.filter(item => item.healthManageId != null)
      console.log(joinList)
      if (joinList.length == 0) {
        this.setData({
          loading: true
        })
        return
      } else {
        this.setData({
          loading: false
        })
      }
      joinList.forEach(items => {
        getFamilyMemberApi({
          familyId: '',
          familyMemberId: items.familyMemberId
        }).then(res => {
          let joinLists = res.data.data.filter(item => items.familyMemberId == item.familyMemberId)
          this.data.citys.push({
            label: joinLists[0].nickName,
            value: joinLists[0].relation
          })
          this.setData({
            citys: this.data.citys
          })

          console.log(this.data.citys)
        })
      })
    })
  },
  // picke 确定
  onPickerChange(e) {
    const {
      key
    } = e.currentTarget.dataset;
    const {
      value,
      label
    } = e.detail;
    console.log('picker change:', e.detail);
    this.setData({
      familyName: label[0],
      personageName: label[1],
    });
  },
  // picke取消
  onPickerCancel(e) {
    const {
      key
    } = e.currentTarget.dataset;
    console.log(e, '取消');
    console.log('picker1 cancel:');
    this.setData({
      [`${key}Visible`]: false,
    });
  },
  onSeasonPicker() {
    this.setData({
      dateVisible: true
    });
  },
  // 查询家庭
  getInfo() {
    this.setData({
      citys: [],
      familys: []
    })
    const familyId = GETSTORAGE('familyId')
    const userId = GETSTORAGE('userId')
    // 默认家庭名字
    searchFamilyName({
      familyId
    }).then(res => {
      this.setData({
        familyName: res.data.data.familyName
      })
    })
    getPersonInfoApi({
      userId
    }).then(res => {
      const {
        familyIds,
      } = res.data.data
      // 通过所有家庭id获取所有家庭名字
      let list = []
      familyIds.forEach(item => {
        searchFamilyName({
          familyId: item
        }).then(res => {
          console.log(res);
          if (item != familyId) {
            list.push({
              label: res.data.data.familyName,
              value: item
            })
          } else {
            list.unshift({
              label: this.data.familyName,
              value: familyId
            })
          }
          this.setData({
            familys: list,
            // familyName: list[0].label
          })
          console.log(list, '所有家庭');
        })
      })
      // 筛选出加入家庭管理的家人
      getHealthManageApi({
        familyId: familyId
      }).then(res => {
        console.log(res, '筛选');
        let joinList = res.data.data.filter(item => item.healthManageId != null)
        console.log(joinList)
        if (joinList.length == 0) {
          this.setData({
            loading: true,
            citys: [],
          })
          return
        }
        joinList.forEach(items => {
          getFamilyMemberApi({
            familyId: '',
            familyMemberId: items.familyMemberId
          }).then(res => {
            let joinLists = res.data.data.filter(item => items.familyMemberId == item.familyMemberId)
            this.setData({
              loading: false
            })
            this.data.citys.push({
              label: joinLists[0].nickName,
              value: joinLists[0].relation
            })
            this.setData({
              citys: this.data.citys,
              personageName: this.data.citys[0].label
            })
            console.log(this.data.citys)
          })
        })
      })
    })
  },
  // 血压
  goPressure() {
    wx.navigateTo({
      url: '/pageEcharts/record/record',
    })
  },
  // 脉搏
  goSphygmus() {
    wx.navigateTo({
      url: '/pageEcharts/pulse/pulse',
    })
  },
  //体温
  goThermometer() {
    wx.navigateTo({
      url: '/pageEcharts/temperature/temperature',
    })

  },
  // 呼吸
  goBreathe() {
    wx.navigateTo({
      url: '/pageEcharts/breathe/breathe',
    })
  },
  // 食谱
  goCookbook() {
    wx.navigateTo({
      url: '/pageEcharts/cookbook/cookbook',
    })
  },
  onLoad(options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */

  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
})